import zh_CN from '@/i18n/zh_CN'
import en_US from '@/i18n/en_US'

type LanguageKey = 'zh_CN' | 'en_US'
type Language = typeof zh_CN | typeof en_US

const languages: Record<LanguageKey, Language> = {
  zh_CN,
  en_US,
}

const lang = sessionStorage.getItem('lang') as LanguageKey | null

export function customTranslate(template: string, replacements: Record<string, string> = {}) {
  const translations = languages[lang || 'zh_CN']
  const templateLower = template.toLowerCase()
  const translationKeys = Object.keys(translations.elements)
  const matchedKey = translationKeys.find((key) => key.toLowerCase() === templateLower)

  template = matchedKey
    ? translations.elements[matchedKey as keyof typeof translations.elements]
    : template

  return template.replace(/{([^}]+)}/g, (_, key) => replacements[key] || '{' + key + '}')
}

export default {
  translate: ['value', customTranslate],
}
